unit uPCList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.Buttons,
  Vcl.ToolWin, Vcl.ExtCtrls, IBQuery;

type
  TPCList_F = class(TForm)
    TreeView1: TTreeView;
    Info_Pesquisa: TToolBar;
    ToolBar3: TToolBar;
    Label31: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Timer_PCs: TTimer;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BitBtn1Click(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Timer_PCsTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  PCList_F: TPCList_F;
  vfOrderTitle : String;


implementation

{$R *.dfm}

uses mdTPulser_u, mdsrv_u, mdtemp_u, uFunctions, gVars_u;

procedure TPCList_F.BitBtn1Click(Sender: TObject);
var
  i: integer;
begin
  //populate the treeview control...
  treeview1.items.clear;
  with treeview1.items do
  begin
    beginupdate;
    for i := 1 to 20 do
    begin

           treeview1.items.AddChild (treeview1.items[i], inttostr(i));
           treeview1.items.Insert(nil, 'What a Sheat!!!');

    end;
    endupdate;
  end;

end;

procedure TPCList_F.BitBtn2Click(Sender: TObject);
var
  tn : TTreeNode;
  cnt : integer;
begin
  TreeView1.Items.Clear;

  for cnt := 0 to 9 do
  begin
    tn := TreeView1.Items.AddChild(nil, IntToStr(cnt));
  end;

end;

procedure TPCList_F.FormClose(Sender: TObject; var Action: TCloseAction);
begin


 Action := caFree;
end;

procedure TPCList_F.Timer_PCsTimer(Sender: TObject);
var
vpQueryServidor: TIBQuery;
begin
  (Sender as TTimer).Enabled:= False;
  if not vgDados.DadosOn then
    StartDados;

  ReConnectServ;

  vpQueryServidor := mdsrv.Qry;
  vpQueryServidor.Close;
  vpQueryServidor.SQL.Text := 'SELECT MAQ_ID, MAQ_IPATUAL, MAQ_IPANT, MAQ_IPMACATUAL, MAQ_IPMACANT, MAQ_NOME, MAQ_DOMINIO, MAQ_DTCAD, MAQ_DTUPD,  '+
                                     'MAQ_FUNCAO, MAQ_USADOPOR, MAQ_USER, MAQ_USERATIVO, MAQ_SO, MAQ_STATUS, MAQ_CPUUSO, MAQ_CPUCLOCK, MAQ_MEMUSO,  '+
                                     'MAQ_MEMQTD, MAQ_DESCRICAO, MAQ_ULTERROR, MAQ_UPDPULSER, '+
                                     'CASE WHEN ( CURRENT_TIMESTAMP - MAQ_UPDPULSER) * 1440 < 5 THEN ''On'' ELSE ''Off'' '+
                                         'END AS XXST FROM MAQUINAS ORDER BY MAQ_NOME';
  vpQueryServidor.Prepare;
  vpQueryServidor.Open;



  TreeView1.Items.Clear;
  vpQueryServidor.First;
  TreeView1.Items.BeginUpdate;
  while not vpQueryServidor.eof do
    Begin
      TreeView1.Items.Add(Nil,vpQueryServidor.FieldByName('MAQ_NOME').AsString + ' - '+
                              vpQueryServidor.FieldByName('MAQ_IPATUAL').AsString + ' [ '+
                              vpQueryServidor.FieldByName('MAQ_USADOPOR').AsString + ' ] ');
       if vpQueryServidor.FieldByName('XXST').AsString='On' then

//         TreeView1.Selected.Item[TreeView1.Selected.Index].ImageIndex:=5
//       else
//          TreeView1.Selected.Item[TreeView1.Selected.Index].ImageIndex:=6;

      //Executa_Script_Local_QryScript(CopyAsInsert(vpQueryServidor,'TEMPMAQUINAS'));
      vpQueryServidor.Next;
    end;
  TreeView1.Items.EndUpdate;
  CommitServ;

//  ReConnectLocal;
//  vfOrderTitle := 'XXST DESC,MAQ_IPATUAL';
//  mdtemp.QdsMaquinas.Close;
//  mdtemp.QdsMaquinas.SelectSQL.Text:= 'SELECT MAQ_ID, MAQ_IPATUAL, MAQ_IPANT, MAQ_IPMACATUAL, MAQ_IPMACANT, MAQ_NOME, MAQ_DOMINIO, MAQ_DTCAD, MAQ_DTUPD,  '+
//                                     'MAQ_FUNCAO, MAQ_USADOPOR, MAQ_USER, MAQ_USERATIVO, MAQ_SO, MAQ_STATUS, MAQ_CPUUSO, MAQ_CPUCLOCK, MAQ_MEMUSO,  '+
//                                     'MAQ_MEMQTD, MAQ_DESCRICAO, MAQ_ULTERROR, MAQ_UPDPULSER, '+
//                                     'CASE WHEN ( CURRENT_TIMESTAMP - MAQ_UPDPULSER) * 1440 < 5 THEN ''On'' ELSE ''Off'' '+
//                                         'END AS XXST FROM TEMPMAQUINAS ORDER BY MAQ_NOME';
//  mdtemp.QdsMaquinas.Prepare;
//  mdtemp.QdsMaquinas.Open;


end;

procedure TPCList_F.TreeView1Click(Sender: TObject);
begin

showmessage('Clicou no: '+TreeView1.Selected.Text);
end;

end.
